package live.cz.dao;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import live.common.PostParam;
import live.common.Verifystring;
import live.cz.entity.Accse;
import live.cz.entity.Order;
import live.cz.entity.Product;
import live.cz.response.XmlFormart;
import live.response.WebServicePay;

import org.apache.commons.codec.digest.DigestUtils;
import org.dom4j.Document;
import org.dom4j.Element;



public class ProductQuery {
	Map<String, String> params = null;
	List<Product> products = null;
	private Order order = null;
	public String returnResponse(){
		String result = null;
		try {
			String verifyString = null;
			params = new LinkedHashMap<String, String>();
			params.put("agentid", PostParam.AGENTID.toString());
			params.put("source", PostParam.SOURCE.toString());
			verifyString = new Verifystring().returnVerifystring(params);
			params.put("verifystring", DigestUtils.md5Hex(verifyString));
			result = new WebServicePay().returnResult(PostParam.PRODUCT.toString(),
					params);
			if (!result.contains("<")) {
				result=null;
			}
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
		//	this.returnResponse();
			e.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}

	public List<Product> returnProducts(String phonenum)
			throws Exception {
		Accse item = new Accsegment().returnAccsegment(phonenum);
		String delaytimes = "5分钟";
		if (item != null) {
			String nodeName = "product";
			StringBuffer xpath = new StringBuffer("//products").append(
					"/*[@value='").append(XmlFormart.encodeUTF(item.getProvincename()))
					.append("']/..").append("/*[@value='").append(
							XmlFormart.encodeUTF(item.getIsptype())).append("']/..")
					.append("/*[@value='").append(
							XmlFormart.encodeUTF("移动电话")).append("']/..")
					.append("/*[@value='").append(
							XmlFormart.encodeUTF(delaytimes)).append("']/..");
			String xmlStr = "";
			xmlStr = XmlFormart.returnProducts(xmlStr);
			Document doc = XmlFormart.xmlToDocument(xmlStr);
			List<Element> list = XmlFormart.returnElements(doc, xpath
					.toString());
			if (list != null && list.size() > 0) {
				products = new ArrayList();
				for (Element e : list) {
					Product product = new Product();
					product.setProdId(XmlFormart.returnXmlNodeValue(e,
							nodeName, "prodId"));
					product.setProdContent((int)Double.parseDouble(XmlFormart
							.returnXmlNodeValue(e, nodeName, "prodContent")));
					product.setProdPrice(Double.parseDouble(XmlFormart
							.returnXmlNodeValue(e, nodeName, "prodPrice")));
					product.setProdIsptype(XmlFormart.returnXmlNodeValue(e,
							nodeName, "prodIsptype"));
					product.setProdProvinceid(XmlFormart.returnXmlNodeValue(e,
							nodeName, "prodProvinceid"));
					product.setProdType(XmlFormart.returnXmlNodeValue(e,
							nodeName, "prodType"));
					product.setProdDelaytimes(XmlFormart.returnXmlNodeValue(e,
							nodeName, "prodDelaytimes"));
					products.add(product);
				}
			}
		}
		return products;
	}
	
	public Order returnProInfo(String prodid) throws Exception {
		String xmlStr = "";
		String nodeName="product";
		xmlStr = XmlFormart.returnProducts(xmlStr);
		Document doc = XmlFormart.xmlToDocument(xmlStr);
		StringBuffer xpath = new StringBuffer("//products").append("/*[@name='prodId' and @value='")
		.append(prodid).append("']/..");
		Element element = (Element)doc.selectSingleNode(xpath.toString().trim());
		if(element!=null){
			order=new Order();
			order.setProdid(prodid);
			order.setProPrice(Integer.valueOf(XmlFormart.returnXmlNodeValue(element,
					nodeName, "prodContent")));
			order.setSalePrice(Double.valueOf(XmlFormart.returnXmlNodeValue(element,
					nodeName, "prodPrice")));
			order.setProdInfo(XmlFormart.returnXmlNodeValue(element,
					nodeName, "prodProvinceid")+XmlFormart.returnXmlNodeValue(element,
					nodeName, "prodIsptype"));
		}
		return order;
	}
}